from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.commons.utils import JsCode

from read_data import *
from kit import *



def pie_plot_ring(all_data):
    row = all_data.row_values(0)[1:]
    pie_ring = Pie()
    for mid in range(1, all_data.nrows):
        row_data = all_data.row_values(mid)[1:]
        pie_ring.add("", [list(z) for z in zip(row, row_data)],
                     center=[choose_X_area(mid) + '%', choose_Y_area(mid)],
                     radius=[60, 70]
                     )
        pie_ring.set_global_opts(title_opts=opts.TitleOpts(title=all_data.name), toolbox_opts=opts.ToolboxOpts())
        pie_ring.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}"))
    pie_ring.render()
    pass


# 单饼图
def pie_plot(all_data):
    row = all_data.row_values(0)
    row_data = all_data.row_values(1)
    c = (
        Pie()
            .add("", [list(z) for z in zip(row, row_data)])
            .set_global_opts(title_opts=opts.TitleOpts(title=all_data.name), toolbox_opts=opts.ToolboxOpts())
            .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
            .render("pie_base.html")
    )


# 多饼图
def pies_plot(all_data):
    row = all_data.row_values(0)[1:]
    col = all_data.col_values(0)[1:]
    pie_ring = Pie()
    for mid in range(1, all_data.nrows):
        row_data = all_data.row_values(mid)[1:]
        pie_ring.add(all_data.row_values(mid)[0], [list(z) for z in zip(row, row_data)],
                     center=[choose_X_area(mid) + '%', choose_Y_area(mid)],
                     radius=[60, 70],
                     )
        pie_ring.set_global_opts(title_opts=opts.TitleOpts(title=all_data.name),
                                 toolbox_opts=opts.ToolboxOpts(),
                                 )

    pie_ring.render()
    pass


def line_plot(all_data):
    row = all_data.row_values(0)[1:]
    col = all_data.col_values(0)[1:]
    line = Line()
    line.add_xaxis(row)
    for mid in range(1, all_data.nrows):
        row_data = all_data.row_values(mid)
        line.add_yaxis(row_data[0], row_data[1:])
    line.set_global_opts(title_opts=opts.TitleOpts(title=all_data.name), toolbox_opts=opts.ToolboxOpts())
    line.render('11.html')


# 玫瑰图
def rose_plot(all_data):
    row = all_data.row_values(0)
    row_data = all_data.row_values(1)

    inner_x_data = row[0:int(all_data.nrows * 0.4)]
    inner_y_data = row_data[0:int(all_data.nrows * 0.4)]
    inner_data_pair = [list(z) for z in zip(inner_x_data, inner_y_data)]

    outer_x_data = row
    outer_y_data = row_data
    outer_data_pair = [list(z) for z in zip(outer_x_data, outer_y_data)]

    (
        Pie(init_opts=opts.InitOpts(width="1000px", height="600px"))
            .add(
            series_name="访问来源",
            data_pair=inner_data_pair,
            radius=[0, "30%"],
            label_opts=opts.LabelOpts(position="inner"),
        )
            .add(
            series_name="访问来源",
            radius=["40%", "55%"],
            data_pair=outer_data_pair,
            label_opts=opts.LabelOpts(
                position="outside",
                formatter="{a|{a}}{abg|}\n{hr|}\n {b|{b}: }{c}  {per|{d}%}  ",
                background_color="#eee",
                border_color="#aaa",
                border_width=1,
                border_radius=4,
                rich={
                    "a": {"color": "#999", "lineHeight": 22, "align": "center"},
                    "abg": {
                        "backgroundColor": "#e3e3e3",
                        "width": "100%",
                        "align": "right",
                        "height": 22,
                        "borderRadius": [4, 4, 0, 0],
                    },
                    "hr": {
                        "borderColor": "#aaa",
                        "width": "100%",
                        "borderWidth": 0.5,
                        "height": 0,
                    },
                    "b": {"fontSize": 16, "lineHeight": 33},
                    "per": {
                        "color": "#eee",
                        "backgroundColor": "#334455",
                        "padding": [2, 4],
                        "borderRadius": 2,
                    },
                },
            ),
        )
            .set_global_opts(legend_opts=opts.LegendOpts(pos_left="left", orient="vertical"),
                             toolbox_opts=opts.ToolboxOpts())
            .set_series_opts(
            tooltip_opts=opts.TooltipOpts(
                trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)"
            )
        )
            .render("nested_pies.html")
    )
